package com.focus.xyz.union.pages.frontend.tables;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseTable;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;

import java.util.List;

/**
 * Created by chenmeng on 2016/4/22.
 */
public class AllProdTable extends BaseTable {

    @FindBy(xpath = "//table[@class='tab-order tab-prod']/tbody")
    private WebElement allProdTable;
    @FindBy(id = "prodName")
    private WebElement prodSearchInput;
    @FindBy(id = "prodNameSearch")
    private WebElement prodSearchBtn;
    @FindBy(xpath = "//a[contains(text(),'立即代理')]")
    private WebElement dlBtn;
    @FindBy(xpath = "//a[contains(text(),'查看推荐产品>')]")
    private WebElement recProdBtn;
    @FindBy(xpath = "//a[contains(text(),'查看所有产品>')]")
    private WebElement allProdBtn;
    @FindBy(xpath = "//a/span[contains(text(),'性价比最高产品')]")
    private WebElement xjbProdBtn;
    @FindBy(xpath = "//a/span[contains(text(),'佣金推荐产品')]")
    private WebElement yjProdBtn;

    @FindBy(xpath = "//a/span[contains(text(),'上一页')]")
    private WebElement prePage;
    @FindBy(xpath = "//a/span[contains(text(),'下一页')]")
    private WebElement nextPage;


    public AllProdTable(WebDriver driver,Action action){
        super(action);
        PageFactory.initElements(driver,this);
    }

    @Override
    protected By getLinkLocator() throws Exception {
        return By.xpath("td[4]/*/a");
    }

    @Override
    protected By getLocator(int index) throws Exception {
        return By.xpath("td[" + index + "]");
    }

    public void inputProd(String prodName) throws Exception{
        action.inputText(prodSearchInput,prodName);
    }

    public void clickSearchBtn() throws Exception{
        action.click(prodSearchBtn);
    }

    public String getStatus(String key) throws Exception{
        return getCellText(allProdTable,key,4);
    }

    public String getStatus1() throws Exception{
        WebElement we = allProdTable.findElement(By.xpath("tr[2]/td[4]"));
        return we.getText();
    }

    /*
    * 精确查询到需要代理的产品再代理
    * */
    public void proxy() throws Exception{
        /*
        * webdriver对隐藏元素的click是无效的。
        * 如需要对隐藏的元素click 可以使用js的click.
        * */
        if(action.contains("对不起，暂无符合条件的产品。")){
            Assert.fail("没有搜索到指定产品!");
        }
        if(!getStatus1().contains("已代理")){
            action.getJsExcutor().executeScript("$('#prod1 > td.r > div > div > a').click()");
        }
        action.pause(4000l);//立即代理之后，状态改变需要等待时间长一点,等待代理状态的刷新。
        String status = (String) action.getJsExcutor().executeScript("return $('#prod1" + "> td.r > div > div > div').text()");
        Assert.assertTrue(status.equals("已代理"));
    }

    /*
    * 查看推荐产品列表
    * */
    public void linkRecProd() throws Exception{
        action.click(recProdBtn);
    }
    /*
    * 查看所有产品列表
    * */
    public void linkAllRrod() throws Exception{
        action.click(allProdBtn);
    }

    /*
    * 代理拥挤推荐产品
    * */
    public void linkYjPROD() throws Exception{
        action.click(yjProdBtn);
    }
    /*
    * 代理性价比推荐产品
    * */
    public void linkXjbPROD() throws Exception{
        action.click(xjbProdBtn);
    }

    protected void prePage() throws Exception{
        action.click(prePage);
    }
    protected void nextPage() throws Exception{
        action.click(nextPage);
    }

    /*
    * 代理并验证是否代理成功
    * */
    public void proxyProd() throws Exception{
        //代理性价比产品
        linkXjbPROD();
        proxyAndVerify();
        //代理佣金产品
        linkYjPROD();
        proxyAndVerify();

    }

    /*
    *代理产品，如果当前页中所有产品都已被代理，则翻页继续查询
    * */
    private void proxyAndVerify() throws Exception{
        boolean flag = true;
        while(flag){
            List<WebElement> rows = allProdTable.findElements(By.tagName(Constants.TAG_TR));
            for(int i = 0;i<rows.size();i++){
                WebElement col = rows.get(i).findElement(By.xpath("td[@class='r']"));
                if(col.getText().contains("已代理")){
                    continue;
                }else{
                    action.getJsExcutor().executeScript("$('#prod" + (i+1) + "> td.r > div > div > a').click()");
                    action.pause(4000l);//立即代理之后，状态改变需要等待时间长一点。
                    String status = (String) action.getJsExcutor().executeScript("return $('#prod" + (i+1) + "> td.r > div > div > div').text()");
                    Assert.assertTrue(status.equals("已代理"));
                    flag = false;
                    break;
                }
            }
            if(flag){
                nextPage();
                action.pause(3000l);//翻页后时间要等待长一点。
            }
        }
    }

}
